package chances.epg.data.taglib;

import java.util.List;
import javax.servlet.jsp.JspException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.data.taglib.page.PageInfo;
import chances.epg.entity.content.Category;

/**
 * 栏目列表.
 * 支持分页查询.
 * @author shitao
 *
 */
public class CategoryListTag extends QueryListTag {
	private static final Logger logger = EpgLogFactory.getSystemLogger();
	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -1206180657029929973L;
	private String parentCode;//父栏目编码
	private int size = 10;//条目数
	
	@Override
	public int doEndTag() throws JspException {
		String pc = this.parentCode;
		
		if(StringUtils.isBlank(pc)){
			return super.doEndTag();
		}
		
		try{
			if(StringUtils.isNotBlank(this.getPageName())){
				this.queryItemByPage(pc);
			}else{
				this.queryItemBySize(pc);
			}
		}catch(Throwable ex){
			logger.error("CategoryListTag error",ex);
		}
		
		return super.doEndTag();
	}
	
	/**
	 * 分页查询.
	 */
	private void queryItemByPage(String categoryCode) {
		PageInfo pageInfo = this.createPageInfo(size);
		List<Category> result = this.getContentService().getCategoryLists(categoryCode, this.start,pageInfo.getPageBean());
		this.setPageInfo(pageInfo);
		this.setCollectionResult(result);
		this.pageNo = 0;
	}
	
	/**
	 * 查询指定数量的条目.
	 */
	private void queryItemBySize(String categoryCode) {
		List<Category> result = this.getContentService().getCategoryLists(categoryCode,this.start,this.size);
		this.setCollectionResult(result);
	}
	
	public String getParentCode() {
		return parentCode;
	}
	public void setParentCode(String parentCode) {
		this.parentCode = parentCode;
	}
	
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
}
